package dao.mysql;
import dao.Entidad;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Comprobante_Pago;
import util.DataBaseConnection;


public class MySQLComprabante_PagoDAO implements Entidad<Comprobante_Pago>{

	@Override
	public Comprobante_Pago insert(Comprobante_Pago t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO comprabante_pago(id_Comp_pago, fecha, hora, descripcion, id_proveedor, id_Atv";
			sql = sql + ") VALUES(?,?,?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getId_comp_pago());
			ps.setDate(2, (java.sql.Date) t.getFecha());
			ps.setDate(3, (java.sql.Date) t.getHora());
			ps.setString(4, t.getDescripcion());
			ps.setInt(5, t.getId_proveedor());
			ps.setInt(6, t.getId_Atv());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_comp_pago(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Comprobante_Pago t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE comprabante_pago SET fecha=?, hora=?, descripcion=?";
			sql = sql +	"WHERE id_Comp_pago=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setDate(1, (java.sql.Date) t.getFecha());
			ps.setDate(1, (java.sql.Date) t.getHora());
			ps.setString(2, t.getDescripcion());
			ps.setInt(3, t.getId_comp_pago());
					
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public int delete(Comprobante_Pago t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM comprabante_pago WHERE id_Comp_pago=? ";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_comp_pago());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Comprobante_Pago findById(Comprobante_Pago t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM comprabante_pago WHERE id_Comp_pago=? ";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_comp_pago());
						
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_comp_pago(rs.getInt("id_Comp_pago"));
				t.setId_proveedor(rs.getInt("id_proveedor"));
				t.setId_Atv(rs.getInt("id_Atv"));
				t.setFecha(rs.getDate("fecha"));
				t.setHora(rs.getDate("hora"));
				t.setDescripcion(rs.getString("descripcion"));
						
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Comprobante_Pago> listAll() {
		List<Comprobante_Pago> comprovante = new ArrayList<Comprobante_Pago>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM comprabante_pago where id_Comp_pago=? and id_proveedor=? and id_proveedor=?";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Comprobante_Pago t = new Comprobante_Pago();
				t.setId_comp_pago(rs.getInt("id_Comp_pago"));
				t.setId_proveedor(rs.getInt("id_proveedor"));
				t.setId_Atv(rs.getInt("id_Atv"));
				t.setFecha(rs.getDate("fecha"));
				t.setHora(rs.getDate("hora"));
				t.setDescripcion(rs.getString("descripcion"));
				comprovante.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return comprovante;
	}

	@Override
	public List<Comprobante_Pago> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

}
